package datos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class CrearBD {

	public static void CargarDriver() {
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();

		} catch (Exception ex) {
			System.out.println("Error Exception loading Driver:" + ex);
		}
	}

	public static void main(String[] args) {
		CargarDriver();

		Connection conexion = null;
		PreparedStatement ps = null;

		String user = ConectarBD.getUser();
		String password = ConectarBD.getPassword();

		String url = "jdbc:mysql://localhost:3306/";

		try {
			conexion = DriverManager.getConnection(url, user, password);
		} catch (Exception ex) {
			System.err.println("Error DriverManager.getConnection(): " + ex);
		}

		String queryCrearBD = "CREATE DATABASE GestionProfesor";

		try {
			ps = (PreparedStatement) conexion.prepareStatement(queryCrearBD);
			ps.executeUpdate();
			System.out
					.println("Exito al crear correctamente la base de datos GestionProfesor");
		} catch (SQLException ex) {
			System.out.println("Error ps.executeQuery(): " + ex);
		}

		String queryAsignarBD = "USE GestionProfesor";

		try {
			ps = (PreparedStatement) conexion.prepareStatement(queryAsignarBD);
			ps.executeUpdate();
			System.out
					.println("Exito al asignar correctamente la base de datos GestionProfesor");
		} catch (SQLException ex) {
			System.out.println("Error ps.executeQuery(): " + ex);
		}

		try {
			ps.close();
			conexion.close();
			CreacionTablas.main(args);
		} catch (SQLException ex) { /* ignore Exception */

		}
	}
}